package com.jiedeng.controller;

import com.jiedeng.dao.*;
import com.jiedeng.model.*;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 覃远龙
 * @ClassName: CheckItemController
 * @Description:
 * @date 2017/4/25 16:46
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Autowired
    ICheckItemRepository checkItemRepository;

    @Autowired
    IPositionRepository positionRepository;

    @Autowired
    INormTypeRepository normTypeRepository;

    @Autowired
    INormRepository normRepository;

    @Autowired
    IRankRepository rankRepository;

    @Autowired
    IWBSRepository iwbsRepository;

    @Autowired
    ISysNamedRepository namedRepository;

    @Autowired
    IPeriodRepository periodRepository;

    //----------------- check table -------------------//
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Page<CheckItem> searchCheckItem(@RequestBody final CheckItem checkItem, int page, int size){
        Specification<CheckItem> sp = new Specification<CheckItem>() {
            @Override
            public Predicate toPredicate(Root<CheckItem> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<Predicate>();
                if(checkItem.getPropId() != null && checkItem.getPropId() > 0){
                    predicates.add(criteriaBuilder.equal(root.get("propId"), checkItem.getPropId()));
                }
                if(checkItem.getNatId() != null && checkItem.getNatId() > 0){
                    predicates.add(criteriaBuilder.equal(root.get("natId"), checkItem.getNatId()));
                }
                if(checkItem.getProTypeId() != null && checkItem.getProTypeId() > 0){
                    predicates.add(criteriaBuilder.equal(root.get("proTypeId"), checkItem.getProTypeId()));
                }
                if(checkItem.getProDegreeId() != null && checkItem.getProDegreeId() > 0){
                    predicates.add(criteriaBuilder.equal(root.get("proDegreeId"), checkItem.getProDegreeId()));
                }
                if(checkItem.getSpecId() != null && checkItem.getSpecId() > 0){
                    predicates.add(criteriaBuilder.equal(root.get("specId"), checkItem.getSpecId()));
                }
                if(checkItem.getPeriodId() != null &&  checkItem.getPeriodId() > 0){
                    predicates.add(criteriaBuilder.equal(root.get("periodId"), checkItem.getPeriodId()));
                }

                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };

        Pageable pageable = PageRequest.of(page, size);
        Page<CheckItem> pageRst = checkItemRepository.findAll(sp, pageable);
        List<CheckItem> checkItemList = pageRst.getContent();
        for (CheckItem item : checkItemList) {
            if(item.getPeriodId() !=null && item.getPeriodId() > 0){
                Period period = periodRepository.getOne(item.getPeriodId());
                if(period != null) item.setPeriodCaption(period.getCaption());
            }
            if(item.getPostId() != null && item.getPostId() > 0){
                Position position = positionRepository.getOne(item.getPostId());
                if(position != null) item.setPostValue(position.getCaption());
            }
            if(item.getNormId() != null && item.getNormId() > 0){

                Norm normalType = normRepository.getOne(item.getNormId());
                if(normalType != null) item.setNormValue(normalType.getCaption());
            }
            if(item.getRankId() != null && item.getRankId() > 0){
                Rank rank = rankRepository.getOne(item.getRankId());
                if(rank != null) item.setRankCaption(rank.getCaption());
            }
            if(item.getWbsId() !=null && item.getWbsId() > 0){
                WBS wbs = iwbsRepository.getOne(item.getWbsId());
                if(wbs != null) item.setWbsValue(wbs.getCaption());
            }
            if(item.getNamed() != null && item.getNamed() > 0){
                SysNamed named = namedRepository.getOne(item.getNamed());
                item.setNamedValue(named.getNameCode());
            }
        }

        return  pageRst;
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<CheckItem> findAll(){
        return checkItemRepository.findAll();
    }

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public CheckItem findCheckItem(long id){
        return checkItemRepository.getOne(id);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CheckItem saveCheckItem(@RequestBody CheckItem rank){
        return checkItemRepository.save(rank);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public void deleteCheckItem(long id){
        checkItemRepository.deleteById(id);
    }

    @RequestMapping(value = "/qiniu/key")
    public String getUploadKey() {
        String accessKey = "8mAy18ftJZ0Napa1N2WJJ4y4n6dZcqpLR7dEIEca";
        String secretKey = "T20ujfKxVjVgyGSrME10DD1ROzejAiFQ3tkHKfu0";
        String bucket = "elecinspection";

        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);

        return upToken;
    }
}
